package com.lovo.roomProject.classManager.service.impl;

import com.lovo.roomProject.classManager.dao.ClassDAO;
import com.lovo.roomProject.classManager.dao.impl.ClassDAOImpl;
import com.lovo.roomProject.classManager.service.ClassService;
import com.lovo.roomProject.pojo.ClaszDO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.student.dao.StudentDAO;
import com.lovo.roomProject.student.dao.impl.StudentDAOImpl;

import java.util.List;

public class ClassServiceImpl implements ClassService {
    ClassDAO classDAO = new ClassDAOImpl();

    @Override
    public List listClass() {
        return classDAO.listClass();
    }

    @Override
    public CutPageBean listClassByPage(int current) {
        return classDAO.listClassByPage(current);
    }

    @Override
    public ClaszDO getClassByName(String name) {
        return classDAO.getClassByName(name);
    }

    @Override
    public ClaszDO getClassById(int id) {
        return classDAO.getClassById(id);
    }

    @Override
    public void insert(ClaszDO claszDO) throws Exception {
        ClaszDO oldClassDO = classDAO.getClassByName(claszDO.getName());
        if (claszDO.getName().equals(oldClassDO.getName())) {
            throw new Exception("已存在相同的" + claszDO.getName());
        }
        classDAO.insert(claszDO);
    }

    @Override
    public void delete(int id) throws Exception{
        //通过id获得班级
        ClaszDO classDO = classDAO.getClassById(id);
        //通过班级名获得班上的学生列表
        StudentDAO studentDAO = new StudentDAOImpl();
      CutPageBean cutPageBean =   studentDAO.listStudentByPage(1,null,classDO.getName(),null);
        if(cutPageBean.getCount()>0){
            throw new Exception("该班级还有学生，不能删除");
        }
        classDAO.delete(id);
    }
}
